/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import gui.event.AddEditEventWindow;
import java.awt.CardLayout;
import java.awt.Desktop;
import java.awt.Toolkit;
import java.io.IOException;
import java.net.URI;
import java.util.ResourceBundle;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import resources.ResourceManager;

/**
 * The main Application Window, contains the start and track panel in a cardlayout, manages the switching and the menubar
 * 
 * @author Pascal
 */
public class MainWindow extends JFrame {

    private static final String START_CARD_NAME = "start";
    private static final String TRACK_CARD_NAME = "track";
    private CardLayout layout;
    private JMenuBar startPanelMenu;
    private JMenu startPanelMenuFile;
    private JMenuItem startPanelMenuItemNewTrack;
    private JMenuItem startPanelMenuItemDeleteTrack;
    private JMenuItem startPanelMenuItemImportTrack;
    private JMenuItem startPanelMenuItemExportTrack;
    private JMenuItem startPanelMenuItemClose;
    private JMenu startPanelMenuEdit;
    private JMenuItem startPanelMenuItemEditTrack;
    private JMenuBar trackPanelMenu;
    private JMenu trackPanelMenuFile;
    private JMenuItem trackPanelMenuItemChooseTrack;
    private JMenuItem trackPanelMenuItemAddEvent;
    private JMenuItem trackPanelMenuItemDeleteEvent;
    private JMenu trackPanelMenuEdit;
    private JMenuItem trackPanelMenuItemEditEvent;
    private StartPanel startPanel;
    private TrackPanel trackPanel;

    public MainWindow() {
        ResourceBundle bundle = ResourceBundle.getBundle("gui/guilabels"); // NOI18N



        initComponents(bundle);

        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/gui/resources/logo.png")));

        setStartPanel();
        //setTrackPanel(new Track("Wandern"));
    }
    
     private void loadHelp() { 
        try {
            URI helpUri = new ResourceManager().copyHelp();
            Desktop.getDesktop().browse(helpUri);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this,
                    ResourceBundle.getBundle("gui/guilabels").getString("erroropeninghelp"),
                    ResourceBundle.getBundle("gui/guilabels").getString("error"),
                    JOptionPane.ERROR_MESSAGE);
        }
    } 


    private void initComponents(ResourceBundle bundle) {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(1100, 800));

        startPanel = new StartPanel(this);
        trackPanel = new TrackPanel();

        layout = new CardLayout();
        getContentPane().setLayout(layout);

        getContentPane().add(startPanel, START_CARD_NAME);
        getContentPane().add(trackPanel, TRACK_CARD_NAME);

        initMenus(bundle);

        pack();
    }

    private void initMenus(ResourceBundle bundle) {
        //initHelpMenu(bundle);
        initStartMenu(bundle);
        initTrackMenu(bundle);
    }

    private void initStartMenu(ResourceBundle bundle) {
        startPanelMenu = new JMenuBar();
        startPanelMenuFile = new JMenu();
        startPanelMenuItemNewTrack = new JMenuItem();
        startPanelMenuItemNewTrack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startPanelMenuItemNewTrackActionPerformed(evt);
            }
        });
        startPanelMenuItemDeleteTrack = new JMenuItem();
        startPanelMenuItemDeleteTrack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startPanelMenuItemDeleteTrackActionPerformed(evt);
            }
        });
        startPanelMenuItemImportTrack = new JMenuItem();
        startPanelMenuItemImportTrack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startPanelMenuItemImportTrackActionPerformed(evt);
            }
        });
        startPanelMenuItemExportTrack = new JMenuItem();
        startPanelMenuItemExportTrack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startPanelMenuItemExportTrackActionPerformed(evt);
            }
        });
        startPanelMenuItemClose = new JMenuItem();
        startPanelMenuItemClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startPanelMenuItemCloseActionPerformed(evt);
            }
        });
        startPanelMenuEdit = new JMenu();
        startPanelMenuItemEditTrack = new JMenuItem();
        startPanelMenuItemEditTrack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startPanelMenuItemEditTrackActionPerformed(evt);
            }
        });

        startPanelMenuFile.setText(bundle.getString("file")); // NOI18N

        startPanelMenuItemNewTrack.setText(bundle.getString("newtrack"));
        startPanelMenuItemNewTrack.setIcon(new ImageIcon(getClass().getResource("/gui/resources/world_add.png")));
        startPanelMenuFile.add(startPanelMenuItemNewTrack);

        startPanelMenuItemDeleteTrack.setText(bundle.getString("deletetrack"));
        startPanelMenuItemDeleteTrack.setIcon(new ImageIcon(getClass().getResource("/gui/resources/world_delete.png")));
        startPanelMenuFile.add(startPanelMenuItemDeleteTrack);

        startPanelMenuFile.add(new JSeparator());

        startPanelMenuItemImportTrack.setText(bundle.getString("importtrack"));
        startPanelMenuItemImportTrack.setIcon(new ImageIcon(getClass().getResource("/gui/resources/world_in.png")));
        startPanelMenuFile.add(startPanelMenuItemImportTrack);

        startPanelMenuItemExportTrack.setText(bundle.getString("exporttrack"));
        startPanelMenuItemExportTrack.setIcon(new ImageIcon(getClass().getResource("/gui/resources/world_go.png")));
        startPanelMenuFile.add(startPanelMenuItemExportTrack);

        startPanelMenuFile.add(new JSeparator());

        startPanelMenuItemClose.setText(bundle.getString("close")); // NOI18N
        startPanelMenuFile.add(startPanelMenuItemClose);

        startPanelMenu.add(startPanelMenuFile);

        startPanelMenuEdit.setText(bundle.getString("edit"));

        startPanelMenuItemEditTrack.setText(bundle.getString("edittrack"));
        startPanelMenuItemEditTrack.setIcon(new ImageIcon(getClass().getResource("/gui/resources/world_edit.png")));
        startPanelMenuEdit.add(startPanelMenuItemEditTrack);

        startPanelMenu.add(startPanelMenuEdit);

        startPanelMenu.add(createHelpMenu(bundle));
    }

    private void initTrackMenu(ResourceBundle bundle) {
        trackPanelMenu = new JMenuBar();
        trackPanelMenuFile = new JMenu();
        trackPanelMenuItemChooseTrack = new JMenuItem();
        trackPanelMenuItemChooseTrack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                trackPanelMenuItemChooseTrackActionPerformed(evt);
            }
        });
        trackPanelMenuItemAddEvent = new JMenuItem();
        trackPanelMenuItemAddEvent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                trackPanelMenuItemAddEventActionPerformed(evt);
            }
        });
        trackPanelMenuItemDeleteEvent = new JMenuItem();
        trackPanelMenuItemDeleteEvent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                trackPanelMenuItemDeleteEventActionPerformed(evt);
            }
        });
        trackPanelMenuEdit = new JMenu();
        trackPanelMenuItemEditEvent = new JMenuItem();
        trackPanelMenuItemEditEvent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                trackPanelMenuItemEditEventActionPerformed(evt);
            }
        });

        trackPanelMenuFile.setText(bundle.getString("file"));

        trackPanelMenuItemChooseTrack.setText(bundle.getString("choosetrack"));
        trackPanelMenuItemAddEvent.setText(bundle.getString("addevent"));
        trackPanelMenuItemAddEvent.setIcon(new ImageIcon(getClass().getResource("/gui/resources/note_add.png")));
        trackPanelMenuItemDeleteEvent.setText(bundle.getString("deleteevent"));
        trackPanelMenuItemDeleteEvent.setIcon(new ImageIcon(getClass().getResource("/gui/resources/note_delete.png")));

        trackPanelMenuFile.add(trackPanelMenuItemChooseTrack);
        trackPanelMenuFile.add(new JSeparator());
        trackPanelMenuFile.add(trackPanelMenuItemAddEvent);
        trackPanelMenuFile.add(trackPanelMenuItemDeleteEvent);

        trackPanelMenuEdit.setText(bundle.getString("edit"));

        trackPanelMenuItemEditEvent.setText(bundle.getString("editevent"));
        trackPanelMenuItemEditEvent.setIcon(new ImageIcon(getClass().getResource("/gui/resources/note_edit.png")));

        trackPanelMenuEdit.add(trackPanelMenuItemEditEvent);

        trackPanelMenu.add(trackPanelMenuFile);
        trackPanelMenu.add(trackPanelMenuEdit);
        trackPanelMenu.add(createHelpMenu(bundle));
    }

    private JMenu createHelpMenu(ResourceBundle bundle) {
        JMenu menuHelp = new JMenu();
        JMenuItem menuItemOpenHelp = new JMenuItem();
        menuItemOpenHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadHelp();
            }
        });
        JMenuItem menuItemCheckUpdate = new JMenuItem();
        menuItemCheckUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemCheckUpdateActionPerformed(evt);
            }
        });
        JMenuItem menuItemAbout = new JMenuItem();
        menuItemAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemAboutActionPerformed(evt);
            }
        });

        menuHelp.setText(bundle.getString("help")); // NOI18N

        menuItemOpenHelp.setText(bundle.getString("openhelp"));
        menuItemCheckUpdate.setText(bundle.getString("checkupdate"));
        menuItemAbout.setText(bundle.getString("about"));

        menuHelp.add(menuItemOpenHelp);
        menuHelp.add(menuItemCheckUpdate);
        menuHelp.add(menuItemAbout);

        return menuHelp;
    }

    public void setStartPanel() {
        ResourceBundle bundle = ResourceBundle.getBundle("gui/guilabels");
        setTitle(bundle.getString("startTitle"));

        layout.show(getContentPane(), START_CARD_NAME);

        setJMenuBar(startPanelMenu);
    }

    public void setTrackPanel(dto.Track track) {
        if (track == null) {
            throw new NullPointerException();
        }

        setTitle(track.getName());
        trackPanel.setTrack(track);

        layout.show(getContentPane(), TRACK_CARD_NAME);

        setJMenuBar(trackPanelMenu);
    }

    private void startPanelMenuItemNewTrackActionPerformed(java.awt.event.ActionEvent evt) {
        startPanel.createNewTrack();
    }

    private void startPanelMenuItemDeleteTrackActionPerformed(java.awt.event.ActionEvent evt) {
        startPanel.removeSelectedTrack();
    }

    private void startPanelMenuItemImportTrackActionPerformed(java.awt.event.ActionEvent evt) {
        JOptionPane.showMessageDialog(this, ResourceBundle.getBundle("gui/guilabels").getString("featurefollows"), ResourceBundle.getBundle("gui/guilabels").getString("info"), JOptionPane.INFORMATION_MESSAGE);
    }

    private void startPanelMenuItemExportTrackActionPerformed(java.awt.event.ActionEvent evt) {
        JOptionPane.showMessageDialog(this, ResourceBundle.getBundle("gui/guilabels").getString("featurefollows"), ResourceBundle.getBundle("gui/guilabels").getString("info"), JOptionPane.INFORMATION_MESSAGE);
    }

    private void startPanelMenuItemCloseActionPerformed(java.awt.event.ActionEvent evt) {
        System.exit(0);
    }

    private void startPanelMenuItemEditTrackActionPerformed(java.awt.event.ActionEvent evt) {
        if (startPanel.getListviewTrackselected() != null) {
            AddEditTrack newTrackDialog = new AddEditTrack(startPanel.getListviewTrackselected());
            newTrackDialog.setVisible(true);
            newTrackDialog.dispose();
        }
    }

    private void trackPanelMenuItemChooseTrackActionPerformed(java.awt.event.ActionEvent evt) {
        setStartPanel();
    }

    private void trackPanelMenuItemAddEventActionPerformed(java.awt.event.ActionEvent evt) {
        trackPanel.createNewEvent();
    }

    private void trackPanelMenuItemDeleteEventActionPerformed(java.awt.event.ActionEvent evt) {
        trackPanel.deleteEvent();
    }

    private void trackPanelMenuItemEditEventActionPerformed(java.awt.event.ActionEvent evt) {
        if (trackPanel.getSelectedEvent() != null) {
            new AddEditEventWindow(trackPanel.getSelectedEvent()).setVisible(true);
        }
    }

    private void menuItemCheckUpdateActionPerformed(java.awt.event.ActionEvent evt) {
        JOptionPane.showMessageDialog(this, ResourceBundle.getBundle("gui/guilabels").getString("featurefollows"), ResourceBundle.getBundle("gui/guilabels").getString("info"), JOptionPane.INFORMATION_MESSAGE);
    }

    private void menuItemAboutActionPerformed(java.awt.event.ActionEvent evt) {
        gui.about.About about = new gui.about.About();
        about.setVisible(true);
    }
}